//
//  OAFlowListViewController.m
//  LongOA
//
//  Created by XuJiuming on 15/10/31.
//  Copyright (c) 2015年 梁文朗. All rights reserved.
//

#import "OAFlowListViewController.h"
#import "GetOAFlowDefRequest.h"
#import "GetOAFlowDefResponse.h"

@interface OAFlowListViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    NSArray* arrList;
    UITableView* _tableView;
}

@end

@implementation OAFlowListViewController
- (void)leftBarClicked:(id)sender {
    [self dismissViewControllerAnimated:NO completion:nil];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor=[UIColor groupTableViewBackgroundColor];
    [self createNavWithTitle:@"点击选择审批流程" createMenuItem:^UIView *(int nIndex)
     {
         float btnwidth=40.0f;
         float btnheight=40.0f;
         if (nIndex ==0)
         {
             UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
             [btn setFrame:CGRectMake(10.0f, 20.0f+(self.navView.height-20 - 40)/2, btnwidth, btnheight)];
             [btn setImage:[UIImage imageNamed:@"nav_back"] forState:UIControlStateNormal];
             [btn addTarget:self action:@selector(leftBarClicked:) forControlEvents:UIControlEventTouchUpInside];
             btn.contentHorizontalAlignment=UIControlContentHorizontalAlignmentLeft;
             return btn;
         }
         
         return nil;
     }];
    
    _tableView=[[UITableView alloc]initWithFrame:CGRectMake(0.0, 64.0, SCREEN_WIDTH, SCREEN_HEIGHT-64) style:UITableViewStylePlain];
    _tableView.delegate=self;
    _tableView.dataSource=self;
    _tableView.backgroundColor=[UIColor clearColor];
    _tableView.separatorStyle=UITableViewCellSeparatorStyleNone;
    _tableView.delaysContentTouches=NO;
    [self.view addSubview:_tableView];
    [self setExtraCellLineHidden:_tableView];
    
    [self getList];
}
- (void)getList{
    NSString* token=[[NSUserDefaults standardUserDefaults] objectForKey:@"token"];
    int setid=[[[NSUserDefaults standardUserDefaults] objectForKey:@"setid"] intValue];
    
    GetOAFlowDefRequest* request = [[GetOAFlowDefRequest alloc] init];
    request.token = token;
    id<ILJClient> clinet = [[OAIpdataBase shareInstance] getPool:setid];
    [clinet Doexecute:request
             RspClass:[GetOAFlowDefResponse class]
               andMsg:@"获取信息..."
              andView:self.view
globalTimelinePostsWithBlock:^(id rsp) {
    GetOAFlowDefResponse* rslt = rsp;
    
    if (rslt.ErrMsg) {
        NSLog(@"%@",rslt.ErrMsg);
    }
    else{
        arrList=rslt.Items;
        
        //NSLog(@"%@",arrList);
        [_tableView reloadData];
    }
}];
}
-(void)setExtraCellLineHidden: (UITableView *)tableView {
    UIView *view =[ [UIView alloc]init];
    view.backgroundColor = [UIColor clearColor];
    [tableView setTableFooterView:view];
    [tableView setTableHeaderView:view];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark TableView delegate
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    //8.0的分割线额外设置
    if ([[[UIDevice currentDevice]systemVersion]floatValue] >= 8.0) {
        if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
            [cell setSeparatorInset:UIEdgeInsetsZero];
        }
        
        if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
            [cell setLayoutMargins:UIEdgeInsetsZero];
        }
        
        if([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)])
            [cell setPreservesSuperviewLayoutMargins:NO];
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [arrList count];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 66.0;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"newCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        //点击之后，好像2-3秒才反应过来,有延迟
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        
        cell.backgroundColor=[UIColor clearColor];
        cell.contentView.backgroundColor=[UIColor clearColor];
        
        UIView* view=[[UIView alloc] initWithFrame:CGRectMake(10.0, 3.0, SCREEN_WIDTH-20.0, 66.0-6.0)];
        view.backgroundColor=[UIColor whiteColor];
        [cell.contentView addSubview:view];
        
        float ffont=12.0;
        UILabel* lab1=[[UILabel alloc] initWithFrame:CGRectMake(5.0, 8.0, 180.0, 22.0)];
        lab1.backgroundColor=[UIColor clearColor];
        lab1.tag=12;
        lab1.textColor=[UIColor darkGrayColor];
        lab1.font=[UIFont systemFontOfSize:ffont];
        [view addSubview:lab1];
        
        UILabel* lab3=[[UILabel alloc] initWithFrame:CGRectMake(5.0, lab1.bottom, 180.0, 22.0)];
        lab3.backgroundColor=[UIColor clearColor];
        lab3.tag=13;
        lab3.textColor=[UIColor darkGrayColor];
        lab3.font=[UIFont systemFontOfSize:ffont];
        [view addSubview:lab3];
        
        UIImageView* imgView1=[[UIImageView alloc] initWithFrame:CGRectMake(view.width-32.0, 0.0,32.0, 32.0)];
        imgView1.image=[UIImage imageNamed:@"ic_oa_flow_regular"];
        imgView1.tag=111;
        [view addSubview:imgView1];
    }
    
    NSDictionary* dict=[arrList objectAtIndex:indexPath.row];
//    cell.imageView.image=[UIImage imageNamed:@"ic_flow"];
//    cell.textLabel.text=[dict objectForKey:@"typename"];
    UILabel* lab12=(UILabel*)[cell viewWithTag:12];
    lab12.text=[dict objectForKey:@"flowname"]; //[NSString stringWithFormat:@"%@[%@]",[dict objectForKey:@"flowname"],[dict objectForKey:@"flowname"]];
  
    UILabel* lab13=(UILabel*)[cell viewWithTag:13];
    lab13.text=[dict objectForKey:@"flowempstrs"];
    
    UIImageView* imgV=(UIImageView*)[cell viewWithTag:111];
    int iitag=[[dict objectForKey:@"if_rivet"] intValue];
    switch (iitag) {
        case 0:
            imgV.image=[UIImage imageNamed:@"ic_oa_flow_regular"];
            break;
        case 1:
            imgV.image=[UIImage imageNamed:@"ic_oa_flow_temp"];
            break;
        case 2:
            imgV.image=[UIImage imageNamed:@"ic_oa_flow_direct"];
            break;
        case 3:
            imgV.image=[UIImage imageNamed:@"ic_oa_flow_free"];
            break;
        default:
            break;
    }

    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // 取消选中状态
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    
    NSDictionary* dict=[arrList objectAtIndex:indexPath.row];
    //NSLog(@"==%@",dict);
    [[NSNotificationCenter defaultCenter] postNotificationName:@"flowSelect" object:dict];
    [self dismissViewControllerAnimated:NO completion:nil];
 
    /*
     {
     billtypead = 0;
     dscrp = "";
     flowempstrs = "abc,\U5458\U5de53";
     flowid = 16;
     flowname = "\U5e76\U884c\U6d41\U7a0b(\U5168\U90e8\U5ba1)";
     flowtype = "";
     "if_rivet" = 0;
     modemp = "";
     opdate = "2015-09-29 14:49:07";
     opemp = "\U8d85\U7ea7\U7528\U6237";
     relation = "";
     sortnum = 004;
     }
     */
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
